<html>
<head><meta charset="utf-8"><title>reborrows? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html">reborrows?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="244348754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244348754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244348754">(Jun 29 2021 at 21:14)</a>:</h4>
<p>it seems difficult to find definitive descriptions of reborrow rules. like reborrows seem privileged with respect to exclusivity of <code>&amp;mut</code> references. and also assignments to existing variables are considered coercion sites, and coercing <code>&amp;mut T</code> to <code>&amp;T</code> counts as a reborrow?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">ra</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="o">*</span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">17</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">ra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ma</span><span class="p">;</span><span class="w"> </span><span class="c1">// reborrow?</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">ra</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="244349186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349186">(Jun 29 2021 at 21:17)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/reference/type-coercions.html#coercion-sites">https://doc.rust-lang.org/nightly/reference/type-coercions.html#coercion-sites</a> doesn't list assignment expressions as coercion sites, but maybe that list isn't meant to be exhaustive?</p>



<a name="244349511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349511">(Jun 29 2021 at 21:20)</a>:</h4>
<p>I think coercion sites are only about typechecking. I think the borrow checker is allowed to assume every use of a reference is a reborrow; the typechecker doesn't have to concern itself with these reborrows because they don't change the type (if you ignore lifetimes, which AFAIK aren't involved in type inference)</p>



<a name="244349621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349621">(Jun 29 2021 at 21:21)</a>:</h4>
<p>does the type checker care about <code>&amp;T</code> vs <code>&amp;mut T</code>?</p>



<a name="244349629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349629">(Jun 29 2021 at 21:21)</a>:</h4>
<p>yes</p>



<a name="244349661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349661">(Jun 29 2021 at 21:21)</a>:</h4>
<p>I guess in that sense it is a coercion</p>



<a name="244349765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349765">(Jun 29 2021 at 21:22)</a>:</h4>
<p>so <code>ra = ma</code> is assigning a <code>&amp;mut i32</code> to a <code>&amp;i32</code> variable, which is a coercion as i understand it, right?</p>



<a name="244349838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349838">(Jun 29 2021 at 21:23)</a>:</h4>
<p>looking at the examples in the reference, it seems to be sites where the target type is known, and I think an assignment to a variable of known type would also count there</p>



<a name="244349866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244349866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244349866">(Jun 29 2021 at 21:23)</a>:</h4>
<p>(ok, maybe it's a <code>&amp;{integer}</code>; dunno if the <code>i32</code> fallback has occurred at that point in type checking)</p>



<a name="244350099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350099">(Jun 29 2021 at 21:25)</a>:</h4>
<p>here's an example showing that you can do other kinds of coercion there too:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_x</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// &lt;- documented as a coercion site</span>
<span class="w">    </span><span class="n">_x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// &lt;- not documented as a coercion site</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="244350496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350496">(Jun 29 2021 at 21:28)</a>:</h4>
<p>thanks. this is looking like an omission in the Reference, then?</p>



<a name="244350502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350502">(Jun 29 2021 at 21:29)</a>:</h4>
<p>yeah I think so</p>



<a name="244350562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350562">(Jun 29 2021 at 21:29)</a>:</h4>
<p>the type of <code>ra</code> might not be fully inferred in my example, but i guess it doesn't have to be if the only coercion involves the <code>mut</code> vs non-<code>mut</code> properties of the reference</p>



<a name="244350575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350575">(Jun 29 2021 at 21:29)</a>:</h4>
<p>By the way, that page also has this example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// For closure with multiple return statements</span>
<span class="kd">let</span><span class="w"> </span><span class="n">clo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">a</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">b</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">c</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">baz</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">clo</span><span class="p">();</span><span class="w"></span>

<span class="c1">// For type checking of function with multiple return statements</span>
<span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="mi">42</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="mi">0</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="mi">1</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">c</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>There are no multiple <code>return</code>s in these examples. I am guessing someone ran rustfmt or clippy and deleted them</p>



<a name="244350789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350789">(Jun 29 2021 at 21:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="411291">Taylor Yu [they/she]</span> <a href="#narrow/stream/122651-general/topic/reborrows.3F/near/244350562">said</a>:</p>
<blockquote>
<p>the type of <code>ra</code> might not be fully inferred in my example, but i guess it doesn't have to be if the only coercion involves the <code>mut</code> vs non-<code>mut</code> properties of the reference</p>
</blockquote>
<p>Yeah, type inference will definitely see through that, it does the <code>&amp;?T -&gt; *const ?T</code> coercion and solves for <code>?T = u8</code></p>



<a name="244350856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350856">(Jun 29 2021 at 21:32)</a>:</h4>
<p>in your example nothing pins down <code>?T</code> but you have a numeral so it does <code>i32</code> defaulting</p>



<a name="244350928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244350928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244350928">(Jun 29 2021 at 21:33)</a>:</h4>
<blockquote>
<p>There are no multiple returns in these examples. I am guessing someone ran rustup or clippy and deleted them</p>
</blockquote>
<p>yeah, because if there aren't <code>return</code> expressions, then the examples are equivalent to the earlier ones involving multiple <code>if</code> branches or <code>match</code> arms</p>



<a name="244351199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351199">(Jun 29 2021 at 21:35)</a>:</h4>
<p>maybe the rule is "places where the compiler has inferred enough type information about the target type to validate whether the coercion is allowed"?</p>



<a name="244351327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351327">(Jun 29 2021 at 21:37)</a>:</h4>
<p>I think you'll have to dive into the rustc source code to get more info about how reborrows work, for example here: <a href="https://github.com/rust-lang/rust/blob/6e0b554619a3bb7e75b3334e97f191af20ef5d76/compiler/rustc_typeck/src/check/coercion.rs#L9-L17">https://github.com/rust-lang/rust/blob/6e0b554619a3bb7e75b3334e97f191af20ef5d76/compiler/rustc_typeck/src/check/coercion.rs#L9-L17</a></p>



<a name="244351463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351463">(Jun 29 2021 at 21:38)</a>:</h4>
<p>I think the introductory line is the most correct and future proof version of the rule:</p>
<blockquote>
<p>A coercion can only occur at certain coercion sites in a program; these are typically places where the desired type is explicit or can be derived by propagation from explicit types (without type inference).</p>
</blockquote>



<a name="244351534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351534">(Jun 29 2021 at 21:39)</a>:</h4>
<p>it seems a bit weird to me to try to enumerate all the places where the type is known. Type inference is complicated</p>



<a name="244351560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351560">(Jun 29 2021 at 21:39)</a>:</h4>
<p>it's unclear whether that would need to be part of a future rust spec</p>



<a name="244351621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351621">(Jun 29 2021 at 21:40)</a>:</h4>
<p>it would</p>



<a name="244351632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351632">(Jun 29 2021 at 21:40)</a>:</h4>
<p>if you want a usable spec at least</p>



<a name="244351672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351672">(Jun 29 2021 at 21:40)</a>:</h4>
<p>so you basically have to specify the whole type inference algorithm?</p>



<a name="244351696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351696">(Jun 29 2021 at 21:41)</a>:</h4>
<p>yeah, absolutely</p>



<a name="244351709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351709">(Jun 29 2021 at 21:41)</a>:</h4>
<p>yeah, it would probably have to describe type inference in enough detail to specify the rules, too. the existing text says "without type inference", which seems to contradict observation, though</p>



<a name="244351711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351711">(Jun 29 2021 at 21:41)</a>:</h4>
<p>otherwise you'd have to way to know if a program is supposed to type check or not</p>



<a name="244351737"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351737" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351737">(Jun 29 2021 at 21:41)</a>:</h4>
<p>this is also the part I'm most afraid of because it's so full of subtle details</p>



<a name="244351797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351797">(Jun 29 2021 at 21:42)</a>:</h4>
<p>what if you overapproximate it and say that the compiler is allowed to spuriously fail?</p>



<a name="244351823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351823">(Jun 29 2021 at 21:42)</a>:</h4>
<p>ah, I think the "without type inference" means that coercions happen only once, in program order, and are not reapplied once all types have been inferred</p>



<a name="244351869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351869">(Jun 29 2021 at 21:43)</a>:</h4>
<p>there is a simple overapproximation of type inference which is "there exists a typing of the program that refines all given type information"</p>



<a name="244351874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351874">(Jun 29 2021 at 21:43)</a>:</h4>
<p>there might be some exceptions to this though</p>



<a name="244351880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351880">(Jun 29 2021 at 21:43)</a>:</h4>
<p>aren't there already programs where the compiler fails to infer types, but a human could solve the constraints by inspection?</p>



<a name="244351908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351908">(Jun 29 2021 at 21:43)</a>:</h4>
<p>yeah, there should be</p>



<a name="244351930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351930">(Jun 29 2021 at 21:43)</a>:</h4>
<p>I think higher-ranked inference is undecidable, which would mean there <em>must</em> be</p>



<a name="244351993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244351993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244351993">(Jun 29 2021 at 21:44)</a>:</h4>
<p>unsure about the details there though</p>



<a name="244352001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244352001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244352001">(Jun 29 2021 at 21:44)</a>:</h4>
<p>i've been frustrated by the apparent inability of type inference to see through the <code>?</code> operator on occasion</p>



<a name="244352022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244352022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244352022">(Jun 29 2021 at 21:44)</a>:</h4>
<p>that's because of <code>Into</code> I think</p>



<a name="244353912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244353912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244353912">(Jun 29 2021 at 22:01)</a>:</h4>
<p>there seem to be repeated claims that Rust's type system is Turing-complete, and this is one purported demonstration<br>
<a href="https://sdleffler.github.io/RustTypeSystemTuringComplete/">https://sdleffler.github.io/RustTypeSystemTuringComplete/</a></p>



<a name="244354047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244354047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244354047">(Jun 29 2021 at 22:02)</a>:</h4>
<p>with traits it is, yeah</p>



<a name="244354065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244354065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244354065">(Jun 29 2021 at 22:03)</a>:</h4>
<p>whether inference itself is, I'm not sure</p>



<a name="244355796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244355796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244355796">(Jun 29 2021 at 22:20)</a>:</h4>
<p><span class="user-mention silent" data-user-id="411291">Taylor Yu [they/she]</span> <a href="#narrow/stream/122651-general/topic/reborrows.3F/near/244352001">said</a>:</p>
<blockquote>
<p>i've been frustrated by the apparent inability of type inference to see through the <code>?</code> operator on occasion</p>
</blockquote>
<p>This is slightly better now in beta+newer, but the <code>From</code> conversion for the error part of results is fundamental to things being underconstrained in things like closures where the return type isn't specified.  There's generally just not a good way for inference to fix this.</p>



<a name="244356050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244356050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244356050">(Jun 29 2021 at 22:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/reborrows.3F/near/244351534">said</a>:</p>
<blockquote>
<p>it seems a bit weird to me to try to enumerate all the places where the type is known. Type inference is complicated</p>
</blockquote>
<p>The problem is that coercion and inference just fundamentally don't play nice together, because the search space explodes if you add in all the possible coercions.</p>
<p>This is why it's pretty common that you have to explicitly <code>.as_slice()</code> if passing a vec to something that takes a generic, rather than a specific type</p>



<a name="244359813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244359813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244359813">(Jun 29 2021 at 23:05)</a>:</h4>
<p>That part makes perfect sense to me. What is surprising is that the reference is trying to precisely document when it happens rather than giving a heuristic explanation, because type inference seems like the sort of thing we don't want to specify very precisely in order to leave room for the implementation to change</p>



<a name="244360281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244360281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244360281">(Jun 29 2021 at 23:10)</a>:</h4>
<p>But we <em>do</em> want to specify it well enough that different implementations will infer the same types.  And because of traits and things like RPIT, that requires a very precise specification of where coercions apply.</p>



<a name="244360376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/reborrows%3F/near/244360376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/reborrows.3F.html#244360376">(Jun 29 2021 at 23:11)</a>:</h4>
<p>The <code>!</code> stabilization problems can show some examples -- like whether <code>Box::new(x)</code> does the unsizing coercion before or after the <code>new</code> call.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>